package com.myccb.app.view.investment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.myccb.app.R;
import com.myccb.app.widget.rollpageview.ColorPointHintView;
import com.myccb.app.widget.rollpageview.LoopPagerAdapter;
import com.myccb.app.widget.rollpageview.RollPagerView;

import java.util.ArrayList;
import java.util.HashMap;

public class InvestmentFragment extends Fragment {

    private static InvestmentFragment instance;

    public static InvestmentFragment getInstance(){
        if (instance==null)
            instance=new InvestmentFragment();
        return instance;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_investment, container, false);

        GridView gridView = view.findViewById(R.id.gridview);
        String[] gridButtons = {"速盈", "基金投资", "理财产品", "结售汇",
        "账户产品", "账户贵金属", "保险", "全部投资理财"};
        int[] gridIcons = {R.mipmap.main_home_kuaidai, R.mipmap.main_home_transfor,
                R.mipmap.main_home_longpay, R.mipmap.main_home_money,
                R.mipmap.main_home_transfor, R.mipmap.main_home_money,
                R.mipmap.main_home_kuaidai, R.mipmap.main_home_longpay};

        ArrayList<HashMap<String, Object>> hashMaps = new ArrayList<>();
        for (int i = 0; i < 8; i ++){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("image", gridIcons[i]);
            hashMap.put("text", gridButtons[i]);
            hashMaps.add(hashMap);
        }

        gridView.setAdapter(new SimpleAdapter(getActivity(),
                hashMaps,
                R.layout.gridlayout,
                new String[]{"image", "text"},
                new int[]{R.id.gridimage, R.id.gridtext}));

        RollPagerView indexAdv = view.findViewById(R.id.myviewpager);

        indexAdv.setHintView(new ColorPointHintView(getContext(),getResources().getColor(R.color.colorPrimary),getResources().getColor(R.color.Grey200)));
        indexAdv.setAdapter(new LoopPagerAdapter(indexAdv) {
            private int[] adImgs={
                    R.mipmap.index_main_home_adv_1, R.mipmap.index_main_home_adv_2,
                    R.mipmap.index_main_home_adv_3, R.mipmap.index_main_home_adv_4
            };
            @Override
            public View getView(ViewGroup container, int position) {
                ImageView view = new ImageView(container.getContext());
                view.setImageResource(adImgs[position]);
                view.setScaleType(ImageView.ScaleType.CENTER_CROP);
                view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                return view;
            }
            @Override
            public int getRealCount() {
                return adImgs.length;
            }
        });

        RecyclerView recyclerView = view.findViewById(R.id.recyclerView_investment);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(new CustomAdapter(getActivity()));
        recyclerView.setNestedScrollingEnabled(false);

        return view;
    }

    /*
    * onStart回调，才能获得实例
    * */
    @SuppressLint("StaticFieldLeak")
    @Override
    public void onStart() {
        super.onStart();
        new AsyncTask<String, Void, Void>(){

            @Override
            protected Void doInBackground(String...strings) {
                DataSet.getZiXun_ARTICLES(strings[0]);
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                LinearLayout linearLayout = getActivity().findViewById(R.id.rootLinearLayout_investment);
                /*
                * 只在初次启动时向Fragment添加
                * */
                if (linearLayout.getChildCount() <= 0) {
                    LayoutInflater layoutInflater = LayoutInflater.from(getActivity());

                    for (int i = 0; i < 3; i++) {
                        LinearLayout ll = (LinearLayout) layoutInflater
                                .inflate(R.layout.listview_investment_zixun, linearLayout, false);

                        ((TextView)ll.findViewById(R.id.textView1)).setText(DataSet.hashMaps.get(i).get("title"));
                        final int finalI = i;
                        ll.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(getActivity(), DetailsActivity.class);
                                intent.putExtra("position", finalI);
                                startActivity(intent);
                            }
                        });
                        linearLayout.addView(ll);
                    }
                }
            }
        }.execute("http://money.hexun.com/zxkd/");

    }
}
